<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use app\admin\controller\Base;
use app\admin\model\Article as ArticleModel;
use thinkphp323\Application\Home\Common;
class Article extends Base{
	public function index(){
		return $this->fetch();
	}

	public function lists(){
		// $list=articleModel::paginate(3);
		$list=db('article')->alias('a')->join('cate c','c.id=a.cateid')->field('a.id,a.title,a.pic,a.author,a.state,c.catename')->paginate(3);
		$this->assign('list',$list);
		return $this->fetch('list');
	}

	/****
	goodsintro()
	****/
	public function goodsintro(){
		$result=mongofunc();
		$resultc=array();
		foreach ($result as $key => $value) {
			if(!isset($resultc[$value['title']])){
				$resultc[$value['title']]=array();
			}
			$resultc[$value['title']][]=array('id'=>$value['_id'],'title'=>$value['title'],'singleinfo'=>$value['singleinfo'][0]);
		}
		$result=$resultc;
		$this->assign('result',$result);
		return $this->fetch('article/list');
	}

	public function add(){
		if (request()->isPost()) {
			$data=[
				'title'=>input('title'),
				'author'=>input('author'),
				'desc'=>input('desc'),
				'keywords'=>input('keywords'),
				'content'=>input('content'),
				'cateid'=>input('cateid'),
				'time'=>time(),
			];
			if (input('state')=='on') {
				$data['state']=1;
			}
			//$_FILES是上传文件的信息
			if ($_FILES['pic']['tmp_name']) {
				$file=request()->file('pic');    //这里$file是一个请求的对象
				$info=$file->move(ROOT_PATH.'public'.DS.'static/uploads');
				// var_dump($info);
				// var_dump($info);exit();
				$data['pic']='/static/uploads/'.$info->getSaveName();
			}
			// $validate= \think\Loader::validate('article');
			// if (!$validate->scene('add')->check($data)) {
			// 	return $this->error($validate->getError());
			// }
			if (Db::name('article')->insert($data)) {
				return $this->success('添加文章成功！','lists');
			}else{
				return $this->error('添加文章失败！');
			}
			return;
		}
		$cateres=db('cate')->select();
		$this->assign('cateres',$cateres);
		return $this->fetch('add');
	}

	public function edit(){
		$id=input('id');
		$res=db('article')->find($id);
		if (request()->isPost()) {
			$data=[
				'id'=>input('id'),
				'title'=>input('title'),
				'author'=>input('author'),
				'desc'=>input('desc'),
				'keywords'=>input('keywords'),
				'content'=>input('content'),
				'time'=>time(),
				'cateid'=>input('cateid')
			];
			if (input('state')=='on') {
				$data['state']=1;
			}
			if ($_FILES['pic']['tmp_name'] && isset($_FILES['pic']['tmp_name'])) {
				$file=request()->file('pic');
				$info=$file->move(ROOT_PATH.'public'.DS.'static/uploads');
				$data['pic']='/static/uploads/'.$info->getSaveName();
			}
			//数据的校验 
			// $validate= \think\Loader::validate('article');
			// if (!$validate->scene('edit')->check($data)) {
			// 	return $this->error($validate->getError());
			// }
			$result=db('article')->update($data);
			if ($result) {
				if ($res['pic']!='' && file_exists(ROOT_PATH.'public'.$res['pic'])) {
					unlink(ROOT_PATH.'public'.$res['pic']);
				}
				return $this->success('修改成功！','lists');
			}else{
				return $this->success('修改失败！');
			}
		}
		$cateres=db('cate')->select();
		// var_dump($res);exit();
		$this->assign('cateres',$cateres);
		$this->assign('res',$res);
		return $this->fetch('edits');
	}

	public function delete(){
		$id=input('id');
		if ($id!=20) {
			$res=db('article')->delete($id);
			if ($res) {
				$this->success("删除成功！",'lists');
			}else{
				$this->success("删除失败！",'');
			}
		}else{
			$this->error('初始化文章不能删除！');
		}
		
	}

}
?>